package wjl.awesome.virus;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import wjl.awesome.virus.data.CsvDao;

import java.io.*;
import java.net.URI;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@SpringBootApplication
@EnableScheduling
@Slf4j
public class ViewApplication {

    @javax.annotation.Resource
    RestTemplateBuilder r;

    private static final String LOCAL_PATH =System.getProperty("user.dir")+File.separator+"src"+File.separator+"main"+ File.separator+"resources"+File.separator+"static"+File.separator;

    public static void main(String[] args) {
        SpringApplication.run(ViewApplication.class, args);
    }
    @Scheduled(cron =" ${wjl.Schedule.updateTime}")
    public void reportCurrentTime() throws IOException {
        RequestEntity<Void> requestEntity = RequestEntity.get(URI.create("https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv"))
                .headers(httpHeaders -> httpHeaders.add("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"))
                .build();
        ResponseEntity<Resource> result=  r.build().exchange(requestEntity, Resource.class);
        Resource body = result.getBody();

        assert body != null;

        log.info("开始下载文件到本地"+body.getFilename());
        @Cleanup InputStream inputStream = body.getInputStream();
        @Cleanup FileOutputStream downloadFile = new FileOutputStream(LOCAL_PATH+body.getFilename());
        try {
            int index;
            byte[] bytes = new byte[1024];

            while ((index = inputStream.read(bytes)) != -1) {
                downloadFile.write(bytes, 0, index);
                downloadFile.flush();
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        log.info("下载完成");
    };

    @Bean
    public CommandLineRunner customizeCommandLineRunner(){
        return args -> {
            RequestEntity<Void> requestEntity = RequestEntity.get(URI.create("https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv"))
                    .headers(httpHeaders -> httpHeaders.add("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"))
                    .build();
            ResponseEntity<Resource> result=  r.build().exchange(requestEntity, Resource.class);
            Resource body = result.getBody();

            assert body != null;

            log.info("开始下载文件到本地"+body.getFilename());
            @Cleanup InputStream inputStream = body.getInputStream();
            @Cleanup FileOutputStream downloadFile = new FileOutputStream(LOCAL_PATH+body.getFilename());
           try {
               int index;
               byte[] bytes = new byte[1024];

               while ((index = inputStream.read(bytes)) != -1) {
                   downloadFile.write(bytes, 0, index);
                   downloadFile.flush();
               }
           }catch (Exception e)
           {
               e.printStackTrace();
           }
            log.info("下载完成");
        };
    }

//    @Bean
//    public HttpMessageConverters fastJsonHttpMessageConverters() {
//
//        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//
//        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//
//        fastConverter.setFastJsonConfig(fastJsonConfig);
//
//        return new HttpMessageConverters(fastConverter);
//    }
}
